<?php
require_once '../Services/util/ConfigUtil.php';
require_once '../Services/util/ServiceUtil.php';
require_once '../Services/util/ThumbHandler.php';

$myUploader = new Uploader();

class Uploader
{
	public static $TYPE_IMAGE 	= "image";
 	public static $TYPE_PDF 	= "pdf";
 	public static $TYPE_SWF 	= "swf";
 	public static $TYPE_TESTCODE = "testcode";
 	
 	public static $BASE_PATH 	= "uploadFiles/";
 	public static $DEST_IMAGE 	= "images/";
 	public static $DEST_PDF		= "pdfs/";
 	public static $DEST_SWF		= "swfs/";
 	public static $DEST_TESTCODE	= "testcodes/";
 	public static $DEST_IMAGE_TN	= "tn/";
 	
 	protected $fileUploader = NULL;
 	protected $category 	= NULL;
 	protected $fileDest 	= "images/";
 	protected $fileFullPath = NULL;
 	
 	protected $myDb;//mysql_connect
	protected $sql;//string
	protected $query;//string
	protected $numRows;//int
	protected $result;//array
 	
	public function Uploader()
	{
		$this->fileUploader = $_REQUEST["username"];
		$this->category		= $_REQUEST["category"];
		
		$this->myDb = null;
		$this->sql = null;
		$this->query = null;
		$this->numRows = 0;
		$this->result = array();
		$this->execute();
	}
	
	protected function execute()
	{
		try {
			$this->myDb = mysql_connect(ConfigUtil::SERVER_NAME(),ConfigUtil::MYSQL_USERNAME(),ConfigUtil::MYSQL_PASSWORD());
			mysql_select_db(ConfigUtil::MYSQL_TABLENAME(),$this->myDb);
			mysql_query("SET NAMES 'utf8'");
//			echo "InitializeDB success!";
			$this->checkUploader($this->fileUploader);//step01
		}catch (Exception $exc)
		{
			trigger_error("InitializeDB error!".$exc);
		}
	}
	//step01
	public function checkUploader($uploader)
	{
		if(!is_null($uploader))
		{
			$email = strtolower(trim($uploader));
			$this->sql = "SELECT * FROM user WHERE user.username='$email'";
			$this->query = mysql_query($this->sql,$this->myDb);
			$this->numRows = mysql_num_rows($this->query);
			if($this->numRows>=1)
			{
				$this->checkCategory($this->category);//step02
			}else
			{
				return "<error>Your wanna upload but your email not exist in our database!</error>";
			}
		}else
		{
			return "<error>Your wanna upload but your email not exist in our database!</error>";
		}
		return NULL;
	}
	//step02
	public function checkCategory($fileCategory)
	{
		if(!is_null($fileCategory))
		{
			//verify upload file type;
			switch ($this->category)
			{
				
				case self::$TYPE_IMAGE:
					$this->fileDest = self::$DEST_IMAGE;
					break;
				case self::$TYPE_PDF:
					$this->fileDest = self::$DEST_PDF;
					break;
				case self::$TYPE_SWF:
					$this->fileDest = self::$DEST_SWF;
					break;
				case self::$TYPE_TESTCODE:
					$this->fileDest = self::$DEST_TESTCODE;
					break;
				default:
					return "<error>Your wanna upload category invalid!</error>";
					break;	
			}
//			echo $this->category;
			$this->executeUpload();//step03;
			
		}else
		{
			return "<error>Your wanna upload category invalid!</error>";
		}
		return NULL;
	}
	
	//step03
	public function executeUpload()
	{
		$fileName = $_FILES["Filedata"]["name"]; 
		$fileNameNew = strpbrk($fileName, '.');
		$filenameFinal=date("Ymd")."_".date("ahis").$fileNameNew;
		$fileSource = $_FILES["Filedata"]["tmp_name"]; 
		$fileNewLocation = self::$BASE_PATH.$this->fileDest.$filenameFinal;
		
		//execute file upload
		if(move_uploaded_file($fileSource,$fileNewLocation))
		{
			echo $fileNewLocation;
			if($this->category==self::$TYPE_IMAGE)
			{
				//execute create thumbnail
				if(file_exists($fileNewLocation))
				{
					try {
						$thumbHandler = new ThumbHandler();
						$thumbHandler->setSrcImg($fileNewLocation);
				    	$thumbHandler->setCutType(1);//
				   		$thumbHandler->setDstImg((self::$BASE_PATH).$this->fileDest.(self::$DEST_IMAGE_TN).$filenameFinal);
				    	$thumbHandler->createImg(32,24);
					}catch (Exception $exc)
					{
						return "<fail>create thumbnail failure!".$exc."</fail>";
					}
				}else 
				{
					return "<fail>thumbnail source file not existed!</fail>";
				}
				
			}
		}else
		{
			return "<fail>upload failure</fail>";
		}
		return NULL;
	}
}

?>